﻿Imports System.Data
Imports System.IO
Imports System.Text.RegularExpressions
Imports GalaSoft.MvvmLight
Imports GalaSoft.MvvmLight.CommandWpf
Imports FL_8x0.Common.SystemData
Imports FL_8x0.Common.Database
Imports FL_8x0.Model

Namespace FL_8x0.Viewmodel

    Public Class UserViewModel : Inherits ViewModelBase

        Private _userList As DataView                        '用户信息视图

        Private _userSettingModel As UserSettingModel        '用户设置参数

        Private _ruleVisibility As Visibility                '用户规则显示
        Private _infoVisibility As Visibility                '用户信息显示

        Private _rowUser As DataRowView                      '当前选中用户行

        Private _userModel As UserModel                      '当前选中用户信息
        Private _userRule As String                          '用户规则


        Private _addUserCommand As RelayCommand             '点击“添加”触发命令
        Private _editUserCommand As RelayCommand            '点击“编辑”触发命令

        Private _saveSettingCommand As RelayCommand         '点击“保存用户设置”触发命令


        Private _confirmCommand As RelayCommand            '点击“确认”触发命令
        Private _cancelCommand As RelayCommand             '点击“取消”触发命令

        Private _isAdd = True                              '操作模式


        Public Sub New()
            FlashShowTable()
            RuleVisibility = Visibility.Visible
            InfoVisibility = Visibility.Collapsed
            AddUserCommand = New RelayCommand(Sub() AddUser())
            EditUserCommand = New RelayCommand(Sub() EditUser())
            CancelCommand = New RelayCommand(Sub() Cancel())
            ConfirmCommand = New RelayCommand(Sub() Confirm())
            SaveSettingCommand = New RelayCommand(Sub() SaveSetting())



            Dim strTextFile As String = g_strStartupPath & "\bin\权限设置说明.txt"
            If File.Exists(strTextFile) Then
                UserRule = My.Computer.FileSystem.ReadAllText(strTextFile, System.Text.Encoding.UTF8)
            End If

            SettingModel = New UserSettingModel(User_ReadPublicValue("密码错误次数限制", "3"),
                                                User_ReadPublicValue("密码锁定时间", "10"),
                                                User_ReadPublicValue("密码更换周期", "60"),
                                                User_ReadPublicValue("无操作锁定时间", "10"))


        End Sub


        '=====================================================================
        '函 数 名: SaveSetting
        '参    数: 
        '返 回 值:
        '功能描述: 保存用户设置
        '=====================================================================
        Private Sub SaveSetting()
            If CheckUserSetting() Then
                User_WritePublicValue("密码错误次数限制", SettingModel.ErrorLimit, "")
                User_WritePublicValue("密码锁定时间", SettingModel.LockTime, "")
                User_WritePublicValue("密码更换周期", SettingModel.ReplacementCycle, "")
                User_WritePublicValue("无操作锁定时间", SettingModel.TimeToLock, "")
            End If
        End Sub

        '=====================================================================
        '函 数 名: Confirm
        '参    数: 
        '返 回 值:
        '功能描述: 用户信息存库
        '=====================================================================
        Private Sub Confirm()
            If CheckUserInfo() Then
                If _isAdd Then
                    User_AddUser(Model)
                Else
                    User_UpdateUser(Model)
                    RowUser = Nothing
                End If
                Cancel()
                FlashShowTable()
            End If
        End Sub

        '=====================================================================
        '函 数 名: EditUser
        '参    数: 
        '返 回 值:
        '功能描述: 根据选中行填充用户信息
        '=====================================================================
        Private Sub EditUser()
            _isAdd = False
            If _rowUser Is Nothing Then
                log.WriteLogError("编辑用户", "请先选择要修改的用户！")
                Return
            End If
            ShowUserInfoItems()
            Dim udtUser As New UserModel With {
                    .Username = _rowUser.Item(0),
                    .Password = _rowUser.Item(1),
                    .ErrorCount = _rowUser(13),
                    .Deadline = _rowUser(15)
                    }
            For i = 3 To 12
                udtUser.PermissionList(i - 3) = _rowUser(i)
            Next
            Model = udtUser
        End Sub

        '=====================================================================
        '函 数 名: AddUser
        '参    数: 
        '返 回 值:
        '功能描述: 添加用户栏
        '=====================================================================
        Private Sub AddUser()
            _isAdd = True
            ShowUserInfoItems()
            Model = New UserModel With {
                .Deadline = Now().Date
                }
        End Sub

        '=====================================================================
        '函 数 名: Cancel
        '参    数: 
        '返 回 值:
        '功能描述: 显示用户规则栏
        '=====================================================================
        Private Sub Cancel()
            RuleVisibility = Visibility.Visible
            InfoVisibility = Visibility.Collapsed
        End Sub

        '=====================================================================
        '函 数 名: FlashShowTable
        '参    数: 
        '返 回 值:
        '功能描述: 显示用户列表
        '=====================================================================
        Public Sub FlashShowTable()
            UserList = User_List()
        End Sub

        '=====================================================================
        '函 数 名: CheckUserInfo
        '参    数: 
        '返 回 值:
        '功能描述: 检查输入有效性
        '=====================================================================
        Private Function CheckUserInfo() As Boolean
            If _isAdd Then
                If Model.Username = "boyi" Then
                    log.WriteLogError("输入参数检查", "[用户名]不能为[boyi]！")
                    Return False
                End If

                If Model.Username = "admin" Then
                    log.WriteLogError("输入参数检查", "[用户名]不能为[admin]！")
                    Return False
                End If

                If Model.Username = "" Then
                    log.WriteLogError("输入参数检查", "[用户名]不能为空！")
                    Return False
                End If

                '验证用户名是否已经存在
                If CheckUserExist(Model.Username) Then
                    log.WriteLogError("输入参数检查", "[用户名]已经存在。")
                    Return False
                End If
            End If

            If Model.Username.Length > 250 Then
                log.WriteLogError("输入参数检查", "[用户名]长度不能超过250个字符！")
                Return False
            End If

            '密码
            If Model.Password.Length < 8 Then
                log.WriteLogError("输入参数检查", "[密码]长度不足8个字符！")
                Return False
            End If

            If Model.Password.Length > 250 Then
                log.WriteLogError("输入参数检查", "[密码]长度不能超过250个字符！")
                Return False
            End If

            Dim rg1 As Regex = New Regex("[A-Z]")
            Dim rg2 As Regex = New Regex("[a-z]")
            Dim rg3 As Regex = New Regex("[0-9]")
            Dim rg4 As Regex = New Regex("[_\~\`\!\@\#\$\%\^\&\*\(\)\+\-\{\}\[\]\\\|\;\:\'\,\.\/\<\>\?]")

            Dim ck1 As Boolean = rg1.IsMatch(Model.Password)
            Dim ck2 As Boolean = rg2.IsMatch(Model.Password)
            Dim ck3 As Boolean = rg3.IsMatch(Model.Password)
            Dim ck4 As Boolean = rg4.IsMatch(Model.Password)

            If Not ((ck1 And ck2 And ck3) Or (ck1 And ck2 And ck4) Or (ck1 And ck3 And ck4) Or (ck2 And ck3 And ck4)) Then
                log.WriteLogError("输入参数检查", "[密码]至少包含[数字]、[大写字母]、[小写字母]、[特殊字符]中的三种。")
                Return False
            End If

            If Not CheckTextNumber("密码错误次数", Model.ErrorCount, 9999, 0) Then
                Return False
            End If
            Return True
        End Function

        '=====================================================================
        '函 数 名: CheckUserSetting
        '参    数: 
        '返 回 值:
        '功能描述: 检查输入有效性
        '=====================================================================
        Private Function CheckUserSetting() As Boolean
            If CheckTextNumber("密码错误次数限制", _userSettingModel.ErrorLimit, 0, 9999) Then
                Return False
            End If
            If CheckTextNumber("密码锁定时间", _userSettingModel.LockTime, 0, 9999) Then
                Return False
            End If
            If CheckTextNumber("密码更换周期", _userSettingModel.ReplacementCycle, 0, 9999) Then
                Return False
            End If
            If CheckTextNumber("无操作锁定时间", _userSettingModel.TimeToLock, 0, 9999) Then
                Return False
            End If
            Return True
        End Function

        '=====================================================================
        '函 数 名: ShowUserInfoItems
        '参    数: 
        '返 回 值:
        '功能描述: 显示用户信息栏
        '=====================================================================
        Private Sub ShowUserInfoItems()
            RuleVisibility = Visibility.Collapsed
            InfoVisibility = Visibility.Visible
        End Sub

        Public Property UserList As DataView
            Get
                Return _userList
            End Get
            Set
                _userList = Value
                RaisePropertyChanged()
            End Set
        End Property

        Public Property AddUserCommand As RelayCommand
            Get
                Return _addUserCommand
            End Get
            Set(value As RelayCommand)
                _addUserCommand = value
            End Set
        End Property

        Public Property EditUserCommand As RelayCommand
            Get
                Return _editUserCommand
            End Get
            Set(value As RelayCommand)
                _editUserCommand = value
            End Set
        End Property

        Public Property CancelCommand As RelayCommand
            Get
                Return _cancelCommand
            End Get
            Set(value As RelayCommand)
                _cancelCommand = value
            End Set
        End Property

        Public Property ConfirmCommand As RelayCommand
            Get
                Return _confirmCommand
            End Get
            Set(value As RelayCommand)
                _confirmCommand = value
            End Set
        End Property

        Public Property RuleVisibility As Visibility
            Get
                Return _ruleVisibility
            End Get
            Set(value As Visibility)
                _ruleVisibility = value
                RaisePropertyChanged()
            End Set
        End Property

        Public Property InfoVisibility As Visibility
            Get
                Return _infoVisibility
            End Get
            Set(value As Visibility)
                _infoVisibility = value
                RaisePropertyChanged()
            End Set
        End Property

        Public Property RowUser As DataRowView
            Get
                Return _rowUser
            End Get
            Set
                _rowUser = Value
                RaisePropertyChanged()
            End Set
        End Property

        Public Property Model As UserModel
            Get
                Return _userModel
            End Get
            Set
                _userModel = Value
                RaisePropertyChanged()
            End Set
        End Property

        Public Property UserRule As String
            Get
                Return _userRule
            End Get
            Set
                _userRule = Value
            End Set
        End Property

        Public Property SettingModel As UserSettingModel
            Get
                Return _userSettingModel
            End Get
            Set
                _userSettingModel = Value
            End Set
        End Property

        Public Property SaveSettingCommand As RelayCommand
            Get
                Return _saveSettingCommand
            End Get
            Set
                _saveSettingCommand = Value
            End Set
        End Property
    End Class
End Namespace